home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / isc-src / util / netupdc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  3.6 KB  |  175 lines

  1. /* @(#) $Header: netupdc.c,v 1.3 91/05/07 18:24:12 deyke Exp $ */
  2.  
  3. /* Net Update Client */
  4.  
  5. #define _HPUX_SOURCE    1
  6.  
  7. #include <sys/types.h>
  8.  
  9. #include <fcntl.h>
  10. /* #include <netinet/in.h> */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/socket.h>
  15. #include <sys/utsname.h>
  16. #include <unistd.h>
  17.  
  18. #if defined(__TURBOC__) || defined(__STDC__)
  19. #define __ARGS(x)       x
  20. #else
  21. #define __ARGS(x)       ()
  22. #endif
  23.  
  24. #include "buildsaddr.h"
  25.  
  26. static void pexit __ARGS((const char *s));
  27. static void doread __ARGS((int fd, char *buf, size_t cnt));
  28. static void dowrite __ARGS((int fd, const char *buf, size_t cnt));
  29. int main __ARGS((int argc, char **argv));
  30.  
  31. /*---------------------------------------------------------------------------*/
  32.  
  33. static void pexit(s)
  34. char *s;
  35. {
  36.   perror(s);
  37.   exit(1);
  38. }
  39.  
  40. /*---------------------------------------------------------------------------*/
  41.  
  42. static void doread(fd, buf, cnt)
  43. int  fd;
  44. char  *buf;
  45. size_t cnt;
  46. {
  47.  
  48.   char  *p = buf;
  49.   int  n;
  50.  
  51.   while (cnt) {
  52.     n = read(fd, p, cnt);
  53.     if (n < 0) pexit("read");
  54.     if (!n) {
  55.       printf("read(): End of file\n");
  56.       exit(1);
  57.     }
  58.     p += n;
  59.     cnt -= n;
  60.   }
  61. }
  62.  
  63. /*---------------------------------------------------------------------------*/
  64.  
  65. static void dowrite(fd, buf, cnt)
  66. int  fd;
  67. char *buf;
  68. size_t cnt;
  69. {
  70.  
  71.   char * p = buf;
  72.   int  n;
  73.  
  74.   while (cnt) {
  75.     n = write(fd, p, cnt);
  76.     if (n <= 0) pexit("write");
  77.     p += n;
  78.     cnt -= n;
  79.   }
  80. }
  81.  
  82. /*---------------------------------------------------------------------------*/
  83.  
  84. int  main(argc, argv)
  85. int  argc;
  86. char  **argv;
  87. {
  88.  
  89.   char  *client;
  90.   char  *server;
  91.   char  buf[1024];
  92.   char  filename[1024];
  93.   int  addrlen;
  94.   int  fdfile;
  95.   int  fdsocket;
  96.   int  filesize;
  97.   int  i;
  98.   int  net_filesize;
  99.   int  net_i;
  100.   struct sockaddr *addr;
  101.   struct utsname utsbuf;
  102.  
  103.   alarm(6 * 3600);
  104.  
  105.   if (uname(&utsbuf)) pexit("uname");
  106.  
  107.   server = (argc < 2) ? "db0sao" : argv[1];
  108.   client = (argc < 3) ? utsbuf.nodename : argv[2];
  109.  
  110.   if (getuid()) {
  111.     printf("%s: Permission denied\n", *argv);
  112.     exit(1);
  113.   }
  114.  
  115.   umask(022);
  116.   putenv("HOME=/users/root");
  117.   putenv("LOGNAME=root");
  118.   putenv("PATH=/bin:/usr/bin:/usr/local/bin:/usr/contrib/bin");
  119.   putenv("SHELL=/bin/sh");
  120.   putenv("TZ=MEZ-1MESZ");
  121.  
  122.   if (chdir("/tcp")) pexit("/tcp");
  123.  
  124.   /** if (!(addr = build_sockaddr("unix:/tcp/sockets/netcmd", &addrlen))) { **/
  125.   if (!(addr = build_sockaddr("*:4718", &addrlen))) {
  126.     printf("build_sockaddr(): Failed\n");
  127.     exit(1);
  128.   }
  129.  
  130.   fdsocket = socket(addr->sa_family, SOCK_STREAM, 0);
  131.   if (fdsocket < 0) pexit("socket");
  132.  
  133.   if (connect(fdsocket, addr, addrlen)) pexit("connect");
  134.  
  135.   strcpy(buf, "binary\n");
  136.   dowrite(fdsocket, buf, strlen(buf));
  137.  
  138.   sprintf(buf, "connect tcp %s 4715\n", server);
  139.   dowrite(fdsocket, buf, strlen(buf));
  140.  
  141.   dowrite(fdsocket, client, strlen(client) + 1);
  142.  
  143.   doread(fdsocket, (char *) &net_filesize, 4);
  144.   filesize = ntohl(net_filesize);
  145.  
  146.   tmpnam(filename);
  147.   fdfile = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  148.   if (fdfile < 0) pexit(filename);
  149.   while (filesize > 0) {
  150.     i = filesize < sizeof(buf) ? filesize : sizeof(buf);
  151.     doread(fdsocket, buf, (unsigned) i);
  152.     dowrite(fdfile, buf, (unsigned) i);
  153.     filesize -= i;
  154.   }
  155.   if (close(fdfile)) pexit("close");
  156.  
  157.   sprintf(buf, "uncompress < %s | sh", filename);
  158.   i = system(buf);
  159.   net_i = htonl(i);
  160.   dowrite(fdsocket, (char *) &net_i, 4);
  161.  
  162.   sprintf(buf, "uncompress < %s > /tmp/netupdc.difs", filename);
  163.   system(buf);
  164.   sprintf(buf, "mailx -s netupdc.difs olaf < /tmp/netupdc.difs");
  165.   system(buf);
  166.   unlink("/tmp/netupdc.difs");
  167.  
  168.   if (unlink(filename)) pexit(filename);
  169.  
  170.   if (!i) system("exec make /usr/local/lib/users");
  171.  
  172.   return i;
  173. }
  174.  
  175.